﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Ah.Testudin.Engine.Actors;
using Ah.Testudin.Engine.Actors.Components;
using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Events.Components;

namespace Ah.Testudin.Engine.Processes
{
    public class AIUpdateProcess : ProcessBase
    {
        #region Private Member Variables

        private IList<AIComponent> _aiComponents;

        #endregion

        #region Constructors

        public AIUpdateProcess(IEventManager eventManager)
        {
            if (eventManager == null)
            {
                throw new ArgumentNullException("eventManager");
            }

            _aiComponents = new List<AIComponent>();

            eventManager.RegisterEventHandler(
                EventType.AIComponentCreated, 
                HandleAIComponentCreated);
        }

        #endregion

        #region ProcessBase

        public override void Update(GameTime gameTime)
        {
            foreach (AIComponent currentAIComponent in _aiComponents)
            {
                currentAIComponent.Update(gameTime);
            }
        }

        #endregion

        #region Private Methods

        private void HandleAIComponentCreated(ITestudinEvent testudinEvent)
        {
            AIComponentCreatedEvent aiComponentCreatedEvent = testudinEvent as AIComponentCreatedEvent;

            if (aiComponentCreatedEvent == null)
            {
                throw new ArgumentException("Expected AIComponentCreatedEvent");
            }

            _aiComponents.Add(aiComponentCreatedEvent.AIComponent);
        }

        #endregion
    }
}
